<template>
  <Tab @goBack="goBack" :tabs="tabs" :current="current">
    <div slot="index">
      <el-form :inline="true" label-width="80px">
        <el-form-item label="讲师姓名">
          <el-input
            v-model="queryParams.nickname"
            placeholder="讲师姓名"
            clearable
            size="small"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>

        <el-form-item label="是否推荐">
          <el-select @keyup.enter.native="handleQuery" v-model="queryParams.recommend" placeholder="是否推荐" clearable
                     size="small">
            <el-option
              v-for="dict in recommendOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            />
          </el-select>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        </el-form-item>

        <el-form-item>
          <el-button type="success" size="mini" @click="handleInsert" v-hasPermi="['college:courseLecturer:add']">添加</el-button>
        </el-form-item>
      </el-form>

      <el-table v-loading="loading" :data="postList">
        <el-table-column label="讲师头像" align="center">
          <template slot-scope="scope">
            <div>
              <el-popover placement="right" title="" trigger="click">
                <img :src="scope.row.avatarpath" alt="">
                <img slot="reference" :src="scope.row.avatarpath" alt="" style="max-height: 50px;max-width: 50px">
              </el-popover>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="讲师姓名" align="center" prop="nickname"/>
        <el-table-column label="头衔" align="center" prop="position"/>
        <el-table-column label="收藏次数" align="center" prop="collectionNum"/>
        <el-table-column label="课程数量" align="center" prop="courseNum"/>
        <el-table-column label="讲师简介" align="center" prop="desc" :show-tooltip-when-overflow="true"/>
        <el-table-column label="讲师推荐" align="center" prop="recommend" :formatter="recommendFormat"/>
        <el-table-column label="创建时间" align="center" prop="createtime" :show-tooltip-when-overflow="true"/>
        <el-table-column label="操作" align="center">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="success"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['college:courseLecturer:edit']"
            >编辑
            </el-button>
            <el-button
              size="mini"
              type="danger"
              @click="handleDelete(scope.row)"
              v-hasPermi="['college:courseLecturer:remove']"
            >删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </div>
    <div slot="add">
      <Add :row="row" @goBack="goBack"></Add>
    </div>
  </Tab>

</template>

<script>
  import {listLecturer, removeLecturer} from "@/api/college/lecturer";
  import Add from "./add";
  import Tab from "@/components/Tab";
  export default {
    components: {Add,Tab},
    data() {
      return {
        current: 'index',
        row: undefined,
        open: false,
        loading: false,
        postList: [],
        title: '',
        total: 0,
        recommendOptions: [{"dictLabel": "是", "dictValue": "1"}, {"dictLabel": "否", "dictValue": "0"}],
        tabs: [
          {index: 'index', name: ''}, {'index': 'add', name: '新增'}
        ],
        queryParams: {
          pageNum: 0,
          pageSize: 10,
          recommend: undefined,
          nickname: undefined
        }
      }
    },
    created() {
      this.getList();
    },
    methods: {
      handleDelete(row) {
        let _this = this;
        this.$confirm(`确定要删除讲师昵称为[${row.nickname}]的记录?删除将找不回此记录.`, "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function () {
          removeLecturer(row.id).then(response => {
            _this.msgSuccess("删除成功");
            _this.getList();
          });
        }).catch(function (e) {
          console.log(e);
        });
      },
      recommendFormat(row, column) {
        return this.selectDictLabel(this.recommendOptions, row[column.property]);
      },
      getList() {
        this.loading = true;
        listLecturer(this.queryDateRange(this.queryParams, this.daterange)).then(response => {
          this.postList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      handleInsert() {
        this.row = undefined;
        this.$set(this.tabs, 1, {index: 'add', name: '新增讲师'});
        this.current = 'add';
      },
      handleUpdate(row) {
        this.row = row;
        this.$set(this.tabs, 1, {index: 'add', name: '编辑讲师'});
        this.current = 'add';
      },
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      goBack() {
        this.current = 'index';
        this.getList();
      }
    }
  }
</script>

<style>

</style>
